//
//  1047. 删除字符串中的所有相邻重复项.swift
//  LeetCodeTrain
//
//  Created by rjb on 2021/7/10.
//  Copyright © 2021 rjb. All rights reserved.
//

import Foundation

class Solution1047 {
    // 题目的意思是遇到两个相同的
    // 这到提的理解有意思
    func removeDuplicates(_ s: String) -> String {
        var stack: [Character] = []
        let array: [Character] = Array(s)
        
        for i in 0..<array.count {
            let top = stack.last
            let item = array[i]
            // 栈顶为空，或者 不与栈顶不同
            if top == nil || top != item {
                stack.append(item)
            } else {
                stack.removeLast()
            }
        }
        return String(stack)
    }
    
    static func test() {
        let str = "abbaca"
        let solution = Solution1047()
        let result = solution.removeDuplicates(str)
        print(result)
    }
}
